<?php
App::uses('AppModel', 'Model');
App::uses('TextResponse', 'Model');
App::uses('ScaledResponse', 'Model');
App::uses('ChoiceResponse', 'Model');
/**
 * Response Model
 *
 * @property SurveyResponse $SurveyResponse
 * @property ChoiceQuestionResponse $ChoiceQuestionResponse
 * @property NumericResponse $NumericResponse
 * @property TextResponse $TextResponse
 */
class Response extends AppModel {

	public $responseTypes = array(
		'TextResponse' => "Text",
		'ChoiceResponse' => "Multiple Choice",
		'NumericResponse' => "Numeric Scale", 
	);

	public function afterFind($results, $primary = false) {
		foreach ($results as &$result) {
			$result['ConcreteResponse'] = null;

			foreach (array_keys($this->responseTypes) as $responseType) {
				if (array_key_exists($responseType, $result) && $result[$responseType]['id'] != null) {
					$newInstance = new $responseType();
					$newInstance->read(null, $result[$responseType]['id']);
					$result['ConcreteResponse'] = $newInstance;
				}
				unset($result[$responseType]);
			}
		}

		return $results;
	}

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'surveyresponse_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'SurveyResponse' => array(
			'className' => 'SurveyResponse',
			'foreignKey' => 'surveyresponse_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Question' => array(
			'className' => 'Question',
			'foreignKey' => 'question_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasOne = array(
		'NumericResponse' => array(
			'className' => 'NumericResponse',
			'foreignKey' => 'response_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'TextResponse' => array(
			'className' => 'TextResponse',
			'foreignKey' => 'response_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	public $hasMany = array(
		'ChoiceQuestionResponse' => array(
			'className' => 'ChoiceQuestionResponse',
			'foreignKey' => 'response_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
	);
}
